package cn.itcast.work.controller.content;


import cn.itcast.work.dao.user.UserMapper;
import cn.itcast.work.pojo.InfoText;
import cn.itcast.work.pojo.TextWhole;
import cn.itcast.work.pojo.Text_show;
import cn.itcast.work.pojo.User;
import cn.itcast.work.service.content.ContentService;
import cn.itcast.work.service.user_service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 分类 博客展示
 */

@Slf4j
@Controller()//添加到ioc容器
@RequestMapping("/content")
public class ContentController {

    @Resource(name = "userService")
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 进入其他博主页面
     *
     * @param phone
     * @param session
     * @return
     */
    @RequestMapping("/intoOther")
    public ModelAndView intoOther(String phone, HttpSession session) {
        log.info("进入博主主页--{}", phone);
        ModelAndView mv = new ModelAndView();

        if (phone.equals(session.getAttribute("phone"))) {

            mv.setViewName("redirect:/blog/showText");
            //本博主
            return mv;
        }
        //查询用户所有信息
        User user = userMapper.selectUser(phone);

        //根据用户信息 查询 status=1 的文章
        List<TextWhole> textWholeList = userMapper.toSeeStatus(phone);
        Random random = new Random();
        String hid = "none";
        if (textWholeList.size() != 0) {
            hid = "d-none";
            //查询 cname mname 将分类标签赋给 textWhole
            for (int i = 0; i < textWholeList.size(); i++) {
                int randomNumber = random.nextInt(1, 12);
                textWholeList.get(i).setNumber(randomNumber);
                InfoText info = userService.selectInf(textWholeList.get(i).getTitle());
                textWholeList.get(i).setMid(info.getMid());
                textWholeList.get(i).setCid(info.getCid());
            }
            //        将文章列表添加到Model中，以便在视图中访问
            mv.addObject("texts", textWholeList);
        }
        if (user.getMotto() == null) {
            mv.addObject("othermoto", "此人未签名");
        } else {
            mv.addObject("othermoto", user.getMotto());
        }
        log.info("博主{},个签{}", user.getUsername(), user.getMotto());
        mv.addObject("hid", hid);
        mv.addObject("otherpic", user.getPic());
        mv.addObject("othername", user.getUsername());
        mv.setViewName("author_home");
        return mv;
    }

    @Resource(name = "contentService")
    private ContentService contentService;

    //显示首页博客
    @RequestMapping("showClass")
    public ResponseEntity<List<Text_show>> showClass(Integer id) {
        log.info("点击分类{}", id);
        //得到主分类id 查询文章
        List<Text_show> list = contentService.selectConText(id);
        if (!list.isEmpty()) {
            return new ResponseEntity<>(list, HttpStatus.OK);
        }
        //否则无反应
        log.error("[controller]:首页执行查询分类博客操作失败");
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


}
